// pages/orders/success.js
var app = getApp();
var orderId = 0;
Page({
  data: {

  },
  onLoad: function (options) {
    orderId = options.id;
  },

  onReady: function () {
    var that = this;
    wx.showLoading({
      title: '订单加载中...',
      mask: true
    });
    app.func.req('orders/show', {
      id: orderId
    }, 'GET', function (res) {
      console.log(res.result)
      var order = res.result;
      that.setData({
        order: order
      });
      wx.hideLoading();
      //获取店铺信息
      app.func.req('shops/show', {
        id: res.result.shop_id
      }, 'GET', function (res) {
        console.log(res)
        that.setData({
          shop: res.result
        });
      }, {
        'token': wx.getStorageSync('token')
      });
    }, {
      token: wx.getStorageSync('token')
    })
  },
  back: function () {
    var that =  this;
    wx.redirectTo({
      url: '/pages/share/share?shop_id=' + that.data.order.shop_id + '&products_share=true'
    });
  },
  pay: function () {
    var that = this;
    app.func.req('pay/xcx', {
      tran_no: that.data.order['tran_no']
    }, 'POST', function (res) {
      var prepay_id = res.result['prepay_id'];
      wx.requestPayment({
        'timeStamp': res.result['timestamp'] + '',
        'nonceStr': res.result['nonce_str'],
        'package': 'prepay_id=' + prepay_id,
        'signType': 'MD5',
        'paySign': res.result['pay_sign'],
        'success': function (res) {
          app.func.req('orders/prepay_id', {
            tran_no: that.data.order['tran_no'],
            prepay_id: prepay_id
          }, 'POST', function(res){
            console.log(res);
          },{
            'token': wx.getStorageSync('token')
          });

          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 2000
          })
        },
        'fail': function (res) {
          wx.showToast({
            title: '支付失败',
            image: '/images/fail.png',
            duration: 2000
          });
        }
      });
    }, {
      'token': wx.getStorageSync('token')
    })
  },
  contact: function () {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.shop.kfphone ? that.data.shop.kfphone : that.data.shop.phone
    })
  }
})
